package com.nassir.chatroom.frame;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import com.nassir.chatroom.client.TalkClient;
import com.nassir.chatroom.socket.Message;
import com.nassir.chatroom.util.FrameUtils;

/**
 * 聊天室主窗体
 * 
 * @author nassir.wen
 * @date 2012-12-3下午5:39:19
 * @Copyright: Copyright (c) 2012
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	private JTextArea receiveBox;// 接收窗口
	private JTextArea sendBox;// 发送窗口
	private JButton jb = new JButton("发送");

	public static Color bg = new Color(150,181,230);
	public static Color boxBg = new Color(240,247,250);

	public MainFrame() {
        super("聊天室");
        init();
        initBtnClick();
    }
	
	/**
	 * 初始化
	 */
	private void init(){
	    Container c = this.getContentPane();
        c.setLayout(null);
        receiveBox = new JTextArea();
        receiveBox.setLineWrap(true);
        receiveBox.setEditable(false);
        receiveBox.setBounds(40, 20, 400, 300);
        receiveBox.setBackground(boxBg);
        JScrollPane jsp = new JScrollPane(receiveBox);
        jsp.setBounds(40, 20, 400, 200);
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        c.add(jsp);
        Font f = new Font("宋体", Font.BOLD, 25);
        sendBox = new JTextArea();
        sendBox.setBounds(40, 240, 400, 100);
        sendBox.setBackground(boxBg);
        sendBox.setLineWrap(true);
        sendBox.setEditable(true);
        sendBox.setFont(f);
        jb.setBounds(360, 350, 80, 30);
        c.add(sendBox);
        c.add(jb);
        c.setBackground(bg);
        this.setSize(480, 420);
        this.setVisible(true);
        this.setResizable(false);
        FrameUtils.setCenter(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        new Message(TalkClient.socketClient, this).start();
        this.getReceiveBox().append("温馨提示：登录成功,欢迎您来到聊天室。" + "\n");
	}
	
	/**
	 * 初始化按钮事件
	 */
	private void initBtnClick() {
	    jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Message.sendMessage(TalkClient.socketClient,sendBox.getText());
                    sendBox.setText("");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    try {
                        Message.sendMessage(TalkClient.socketClient,sendBox.getText());
                        sendBox.setText("");
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
	}
	
	public JTextArea getReceiveBox() {
		return receiveBox;
	}

	public void setReceiveBox(JTextArea receiveBox) {
		this.receiveBox = receiveBox;
	}

	public JTextArea getSendBox() {
		return sendBox;
	}

	public void setSendBox(JTextArea sendBox) {
		this.sendBox = sendBox;
	}

}
